<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_gallery extends CI_Model{
    var $table          = "tbl_gallery";
    var $table_link     = "tbl_gallery_link";
    var $primary        = "id";
    
    public function __construct()
    {
        parent::__construct();
    }
    
    function get_all_image(){
        $q = $this->db->query("SELECT * FROM " . $this->table . " WHERE type = 'i'");
        return $q->result();
    }
    
    function get_all_video(){
        $q = $this->db->query("SELECT * FROM " . $this->table . " WHERE type = 'v'");
        return $q->result();
    }
    
    function get_all_images_data($limit=0, $offset=0, $conditions=''){
        if( !empty($conditions) ){
            $conditions = str_replace("%title%",        "title", $conditions);
        }
        
        $sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table . ' WHERE type = "i" ';

        if( !empty($conditions) ){
            $sql .= 'AND ' . $conditions . ' ';
        }
        $sql .= 'ORDER BY datecreated DESC';
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query = $this->db->query($sql);
        if(!$query || !$query->num_rows()) return false;
        
        return $query->result();
    }
    
    function get_videodata($id){
        if ( !is_numeric($id) ) return false;

        $id = absint($id);
        if ( !$id ) return false;
        
        $query = $this->db->get_where($this->table, array('id' => $id));
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $video = $row;
        }
        
        return $video;
    }
    
    function get_all_gallery(){
        $q = $this->db->query("SELECT * FROM `" . $this->table."`");
        return $q->result();
    }
    
    function insert_gallery($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table, $data) ) {
            $id = $this->db->insert_id();
            return $id;
        };
        return false;
    }
    
    function save_data($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table,$data) ) return true;
        return false;
    }
    
    function save_data_link($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_link, $data) ) return true;
        return false;
    }
    
    function update_gallery($data, $id){
        if( empty($id) ) return false;
        if( empty($data) ) return false;
        if( $this->db->update($this->table, $data, array($this->primary => $id)) ) {return TRUE;}
        return false;
    }
    
    function delete($id){
        if ( !is_numeric($id) ) return false;

        $id = absint($id);
        if ( !$id ) return false;
        
        if( $this->db->delete($this->table, array($this->primary => $id)) ){
            $this->db->delete($this->table_link, array($this->primary => $id));
            return true;
        }else{ return false; } 
    }
    
    function get_all(){
        $q = $this->db->query("
            SELECT D.id_donasi,M.name,D.datecreated,D.status,D.jumlah_donasi,D.deleted FROM `" . $this->table . "` as D INNER JOIN `".$this->table_member."` as M ON D.id_member = M.id_member");
        return $q->result();
    }
    
    function get_gallery_by_id($id,$mod = "result"){
        if ( !is_numeric($id) ) return false;

        $id = absint($id);
        if ( !$id ) return false;
        
        $q = $this->db->query("
            SELECT 
                *, A.id, A.type, B.type AS link_type FROM `". $this->table ."` as A 
            LEFT JOIN ". $this->table_link ." as B 
            ON B.id_gallery = A.id 
            WHERE A.id = ".$id."");
            
        if ( !$q->num_rows() ) return false;
            
        if($mod == "result") return $q->result(); 
        
        return $q->row();
    }
    
    function get_gallery_confirmation_by_id($id){
        $q = $this->db->query("SELECT * FROM " . $this->table_conf . " WHERE id = ".$id."");
        return $q->result();
    }
    
    function get_data_by_condition($condition="",$mode="result"){
        $q = $this->db->query("SELECT * FROM `".$this->table."` WHERE ".$condition);
        if(strtolower($mode) == "result") return $q->result();
        else return $q->num_rows();
    }
}
/* End of file model_gallery.php */
/* Location: ./application/models/model_gallery.php */
